/*
 * pps_bom
 */
package com.pitop.mould.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MouldException;
import com.pitop.core.tools.StringTools;
import com.pitop.mould.dao.MouldDAO;
import com.pitop.mould.entity.Mould;
import com.pitop.mould.entity.MouldState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("mouldSV")
@Slf4j
public class MouldSVImpl extends BaseSVImpl<Mould, Long> implements MouldSV {

    @Autowired
    private MouldDAO mouldDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return mouldDAO;
    }

    /**
     * 保存account对象
     * @param entity 实体
     *
     */
    @Override
    public void save(Mould entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setState(MouldState.Develop.name());
        super.save(entity);
    }

    /**
     * 加载一个对象Mould
     *
     * @param id
     * @param code 编码
     * @return Mould
     */
    @Override
    public Mould load(Long id, String code) {
        if (id == null && code == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return mouldDAO.load(param);
    }

    /**
     * 加载一个对象Mould,(将查询关联数据)
     *
     * @param id
     * @param code 编码
     * @return Mould
     */
    @Override
    public Mould get(Long id, String code) {
        if (id == null && code == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return mouldDAO.getDetail(id, code);
    }

    /**
     * 加载一个对象Mould 通过id
     *
     * @param id
     * @return Mould
     */
    @Override
    public Mould loadById(Long id) {
        if (id == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldDAO.loadById(id);
    }

    /**
     * 加载一个对象Mould 通过code
     *
     * @param code 编码
     * @return Mould
     */
    @Override
    public Mould loadByCode(String code) {
        if (code == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldDAO.loadByCode(code);
    }

    /**
     * 加载一个对象Mould 通过id,(将查询关联数据)
     *
     * @param id
     * @return Mould
     */
    @Override
    public Mould getById(Long id) {
        if (id == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldDAO.getById(id);
    }

    /**
     * 加载一个对象Mould 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Mould
     */
    @Override
    public Mould getByCode(String code) {
        if (code == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldDAO.getByCode(code);
    }

    /**
     * 根据主键id,oldStates 共同更新 Mould 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, MouldState newState, MouldState... oldStates) {
        if (id == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        mouldDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 Mould 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(String code, MouldState newState, MouldState... oldStates) {
        if (code == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        mouldDAO.updateStateByCode(code, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 Mould 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, MouldState state) {
        if (id == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        mouldDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键code 更新 Mould 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(String code, MouldState state) {
        if (code == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        mouldDAO.updateByCode(code, state.name(), new Date());
    }


    /**
     * 删除对象Mould
     *
     * @param id * @param code 编码
     * @return Mould
     */
    @Override
    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        mouldDAO.delete(param);
    }


    /**
     * 查询Mould分页
     *
     * @param mould  对象
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Mould>
     */
    @Override
    public List<Mould> list(Mould mould, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (mould != null) {
            map = JSON.parseObject(JSON.toJSONString(mould, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return mouldDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(Mould mould) {
        Map<String, Object> map = null;
        if (mould != null) {
            map = JSON.parseObject(JSON.toJSONString(mould, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return mouldDAO.count(map);
    }

    /**
     * 查询Mould分页
     *
     * @param id     * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Mould>
     */
    @Override
    public List<Mould> list(Long id, String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return mouldDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return mouldDAO.count(param);
    }

    @Override
    public void modify(Mould mould) {
        if(mould == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if(StringTools.isEmpty(mould.getCode())) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Mould mouldFlag = mouldDAO.loadByCode(mould.getCode());
        if(mouldFlag == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //mould是稳定状态
        if(MouldState.Stable == MouldState.getEnum(mouldFlag.getState())) {
            throw new MouldException(BaseException.BaseExceptionEnum.Mould_Is_Stable);
        }
        //mould是停用状态
        if(MouldState.Stop == MouldState.getEnum(mouldFlag.getState())) {
            throw new MouldException(BaseException.BaseExceptionEnum.Mould_Is_Stop);
        }
        mouldDAO.update(mould);

    }



}
